/**
 * loading.js v1.0.0
 * (c) 2017 yelixiao
 *	_init:显示
 *  _clear:隐藏||删除
 **/
import _loadingGif from '@/assets/images/GIF-180.gif'
(function () {
	'use strict';
	var is_mobile = !!navigator.userAgent.match(/mobile/i),
		is_android = !!navigator.userAgent.match(/android/i),
		defaultOptions = {
			view: '',
			style: {
				maskColor: 'rgba(245,246,248,1)',
				font: ".15rem 华文黑体,Helvetica,Arial,Heiti SC,'Microsoft YaHei',STXihei,SimSun",
				text_color: 'rgba(51,51,51,1)'
			},
			text: '努力加载中...',
			index: 99999,
			img: _loadingGif
		};

	//创建构造函数
	function Loading() { }
	var L = Loading.prototype;
	L.constructor = Loading;
	L._init = function () {
		this._options = extend(true, {}, defaultOptions, {})
		var self = this,
			options = this._options;
		if ($('.loading')) return;
		if (this._$container) {
			this.show();
		} else {
			this._initElements();
		}
	};
	L._initElements = function () {
		// 初始化容器
		var _options = this._options,
			$container = createElement($('html'), 'loading', {
				'position': 'absolute',
				'left': '0',
				'top': '0',
				'opacity': '1',
				'width': '100%',
				'height': '100%',
				'z-index': _options.index,
				'background-color': _options.style.maskColor
			});
		this._$container = $container;

		css($container, {
			'user-select': 'none',
			'overflow': 'hidden',
		});

		this._$img_container = createElement($container, 'img_container', {
			'position': 'absolute',
			'left': '50%',
			'top': '1.35rem',
			'height': "1.2rem",
			'width': "1.2rem",
			'margin-top': '0',
			'margin-left': '-.6rem',
			'background-image': "url('" + _options.img + "')",
			'background-repeat': 'no-repeat',
			'background-size': '100%'
		});
		/*this._$img = function(){
			var img=document.createElement('img')
				img.src = _options.img;
				this._$img_container.appendChild(img);
				return img;
		};
		css(this._$img(), {
			'height':"120px",
			 'width':"120px",
		});*/
		this._$text_container = createElement($container, 'text_container', {
			'position': 'absolute',
			'left': '50%',
			'top': '2.55rem',
			'width': '100%',
			'margin-left': '-50%',
			'text-align': 'center',
			'line-height': '0.18rem',
			'color': _options.style.text_color,
			'font': _options.style.font
		});
		this._$text_container.innerHTML = _options.text;
	};
	L.show = function () {
		if (this._$container) {
			$('html').appendChild(this._$container);
		} else {
			this._initElements();
		}
	}
	L._clear = function () {
		if (!this._$container) return;
		removeElement(this._$container);
	};

	function extend() {
		var options, name, src, copy, copyIsArray, clone,
			target = arguments[0] || {},
			targetType = typeof target,
			toString = Object.prototype.toString,
			i = 1,
			length = arguments.length,
			deep = false;

		// 处理深拷贝
		if (targetType === 'boolean') {
			deep = target;

			// Skip the boolean and the target
			target = arguments[i] || {};
			targetType = typeof target;
			i++;
		}

		// Handle case when target is a string or something (possible in deep copy)
		if (targetType !== 'object' && targetType !== 'function') {
			target = {};
		}

		// 如果到此没有更多参数，则表示将 target 扩展给当前函数的持有者
		if (i === length) {
			target = this;
			i--;
		}

		for (; i < length; i++) {

			// Only deal with non-null/undefined values
			if ((options = arguments[i]) != null) {

				// Extend the base object
				for (name in options) {
					src = target[name];
					copy = options[name];

					// 防止死循环
					if (target === copy) {
						continue;
					}

					// 深拷贝对象或者数组
					if (deep && copy &&
						(copyIsArray = toString.call(copy) === '[object Array]') ||
						(toString.call(copy) === '[object Object]')) {

						if (copyIsArray) {
							copyIsArray = false;
							src = src && (toString.call(src) === '[object Array]') ? src : [];

						} else {
							src = src && (toString.call(src) === '[object Object]') ? src : {};
						}

						target[name] = extend(deep, src, copy);


					} else if (copy !== undefined) { // 仅忽略未定义的值
						target[name] = copy;
					}
				}
			}
		}

		// Return the modified object
		return target;
	}
	// 创建元素
	function createElement(parentNode, className, id, prop) {
		var elem = document.createElement('DIV');
		if (typeof className === 'object') {
			prop = className;
			className = null;
		}

		if (typeof id === 'object') {
			prop = id;
			id = null;
		}

		if (typeof prop === 'object') {
			for (var p in prop) {
				elem.style[p] = prop[p];
			}
		}
		if (className) elem.className = className;
		if (id) elem.id = id;

		parentNode.appendChild(elem);
		return elem;
	}

	// 移除元素
	function removeElement(elem) {
		var prop = {
			opacity: 0,
			transition: 'opacity 1s',
			'-moz-transition': 'opacity 1s',
			/* Firefox 4 */
			'-webkit-transition': ' opacity 1s',
			/* Safari 和 Chrome */
			'-o-transition': ' opacity 1s'
		}
		for (var p in prop) {
			elem.style[p] = prop[p];
		}
		setTimeout(function () {
			elem.parentNode && elem.parentNode.removeChild(elem);
			elem.style['opacity'] = 1;
		}, 1100)

	}

	// 获取元素（IE8及以上浏览器）
	function $(selector, context) {
		if (selector instanceof HTMLElement) {
			return selector;
		} else if (!selector || typeof selector !== 'string') {
			return null;
		}

		if (typeof context === 'string') {
			context = document.querySelector(context);
		}

		if (!(context instanceof HTMLElement)) {
			context = document;
		}

		return context.querySelector(selector);
	}

	// 设置属性
	function attr(elem, prop, value) {
		if (typeof prop === 'object') {
			for (var p in prop) {
				elem[p] = prop[p];
			}
			return elem;
		}

		if (value === undefined) {
			return elem[prop];
		} else {
			elem[prop] = value;
			return elem;
		}
	}

	// 设置样式
	function css(elem, prop, value) {
		if (typeof prop === 'object') {
			for (var p in prop) {
				value = prop[p];
				if (isNumber(value)) value += 'px';
				elem.style[p] = value;
			}
			return elem;
		}

		if (value === undefined) {
			return window.getComputedStyle(elem)[prop];
		} else {
			if (isNumber(value)) value += 'px';
			elem.style[prop] = value;
			return elem;
		}
	}

	function support(prop) {
		var testElem = document.documentElement;
		if (prop in testElem.style) return '';

		var testProp = prop.charAt(0).toUpperCase() + prop.substr(1),
			prefixs = ['Webkit', 'Moz', 'ms', 'O'];

		for (var i = 0, prefix; prefix = prefixs[i++];) {
			if ((prefix + testProp) in testElem.style) {
				return '-' + prefix.toLowerCase() + '-';
			}
		}
	}
	// 判断对象是否为数字
	function isNumber(obj) {
		return typeof obj === 'number';
	}
	window.loading = L;
	//loading._init();
	// window.loading_time=setTimeout(function(){
	// 	loading._clear();
	// },20000);
})();